//
//  HMTabBar.swift
//  新浪微博--song
//
//  Created by 宋海保 on 16/4/8.
//  Copyright © 2016年 宋海保. All rights reserved.
//

import UIKit

class HMTabBar: UITabBar {

    //1.定义闭包属性
    var composeClosure: (() ->())?
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        
        //宽度
        let width = self.frame.size.width / CGFloat(HMMainTabBarButtonCount)
        
        var index = 0
        
        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subview.frame = CGRect(x:CGFloat(index)*width, y: 0, width: width, height: frame.size.height)
                index++
                if index == 2 {
                    index++
                    
                }
            }
        }
        
        //计算
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.size.height)
        
    }
    
    //MARK: - 按钮点击事件
    // @objc: 让OC可以调用我们swift的方法,系统默认会为所有的swift方法加上@objc,如果方法加了private,就不会帮我们加@objc
    @objc private func didClickcomposeButton() {
        // View 弹不出另外的控制器,点击事件需要告诉外面的控制器
        composeClosure?()
    }
    
    
    //MARK: - 懒加载
    //加号按钮
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.addTarget(self, action: Selector("didClickcomposeButton") , forControlEvents: .TouchUpInside)
        
        //添加按钮到tabBar
        self.addSubview(button)
        
        return button
    }()

}
